package cn.ito.zjgsu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.ito.zjgsu.domain.DepartmentPerson;
import cn.ito.zjgsu.domain.PersonInfo;
import cn.ito.zjgsu.mapping.DepartmentPersonMapper;
import cn.ito.zjgsu.mapping.PersonInfoMapper;
import cn.ito.zjgsu.service.DepartmentPersonService;

@Service
@Transactional
public class DepartmentPersonServeiceImpl implements DepartmentPersonService {

	@Autowired
	DepartmentPersonMapper departPersonMapper;
	
	@Autowired
	PersonInfoMapper personMapper;
	
	@Override
	public void addStaff(int departmentId, int staffId) {
		DepartmentPerson departmentPerson = new DepartmentPerson();
		departmentPerson.setPersonid(staffId);
		departmentPerson.setDepartmentid(departmentId);
		departPersonMapper.insertSelective(departmentPerson);
	}

	@Override
	public void deleteStaff(int departmentId, int staffId) {
		departPersonMapper.delete(departmentId, staffId);
	}

	@Override
	public List<PersonInfo> findStaff(int departmentId) {
		List<DepartmentPerson> depPerson = departPersonMapper.selectByDep(departmentId);
		List<PersonInfo> persons = new ArrayList<PersonInfo>();
		for(DepartmentPerson dp : depPerson) {
			PersonInfo person = personMapper.selectByPrimaryKey(dp.getPersonid());
			persons.add(person);
		}
		return persons;
	}

}
